package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.exception.BusinessException;
import com.sd365.common.core.common.exception.code.BusinessErrorCode;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.common.util.BeanUtil;
import com.sd365.permission.centre.api.AdministrativeApi;
import com.sd365.permission.centre.entity.Administrative;
import com.sd365.permission.centre.pojo.dto.AdministrativeDTO;
import com.sd365.permission.centre.pojo.query.AdministrativeQuery;
import com.sd365.permission.centre.pojo.query.IdVersionQuery;
import com.sd365.permission.centre.pojo.vo.AdministrativeVO;
import com.sd365.permission.centre.service.AdministrativeService;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
public class AdministrativeController implements AdministrativeApi {

    @Resource
    AdministrativeService administrativeService;
    @Override
    public Boolean add(@Valid AdministrativeDTO administrativeDTO) {
        return administrativeService.add(administrativeDTO);
    }

    @Override
    public Boolean remove(Long id, Long version) {
            return administrativeService.remove(id, version);
    }

    @Override
    public Boolean modify(@Valid AdministrativeDTO administrativeDTO) {
        return administrativeService.modify(administrativeDTO);
    }

    /**
     * 通用查询，带分页信息
     *
     * @param administrativeQuery
     * @return
     */
    @Override
    @ApiLog
    public List<AdministrativeVO> commonQuery(AdministrativeQuery administrativeQuery) {
        List<Administrative> administratives = administrativeService.commonQuery(administrativeQuery);
        List<AdministrativeVO> administrativeVOS;
        try {
            administrativeVOS = BeanUtil.copyList(administratives, AdministrativeVO.class);
        } catch (Exception e) {
            throw new BusinessException(BusinessErrorCode.SYSTEM_BEAN_COPY_EXCEPTION, e);
        }
        return administrativeVOS;
    }

    @Override
    @ApiLog
    public AdministrativeVO selectById(Long id) {
        AdministrativeDTO administrativeDTO = administrativeService.queryById(id);
        if (administrativeDTO != null) {
            AdministrativeVO administrativeVO = BeanUtil.copy(administrativeDTO, AdministrativeVO.class);
            System.out.println(administrativeVO);
            return administrativeVO;
        } else {
            return null;
        }
    }

    @Override
    @ApiLog
    public Boolean batchDelete(@Valid List<IdVersionQuery> idVersionQueryList) {
        return administrativeService.batchDelete(idVersionQueryList);
    }


    @Override
    public Boolean status(AdministrativeDTO administrativeDTO) {
        return administrativeService.batchModify(administrativeDTO);
    }
}
